package com.zk.algorithm.math;

import com.zk.algorithm.annotation.LeetCode;

/**
 * 数值 x 的 n 次方
 *
 * @author zk
 */
@LeetCode
public class PowerOfN {

    public double pow(double x, int n) {
        if (n == 0) {
            return 1;
        }

        if (n == 1) {
            return x;
        }

        if (n == -1) {
            return 1 / x;
        }

        double num = pow(x, n / 2);
        return num * num * pow(x, n % 2);
    }

}
